package MainControl;

import Tool.GetDiffList;
import Tool.GlobalVariable;
import Tool.WriteLog;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class CommandControl implements Runnable {
    private ServerSocket getCommandServer;
    private Socket getCommandSocket;
    private InputStream getCommandStream;
    private volatile static CommandControl commandControl;

    private CommandControl() {
        try {
            getCommandServer = new ServerSocket(7001);
            getCommandSocket = getCommandServer.accept();
            getCommandStream = getCommandSocket.getInputStream();
            System.out.println("InitFinish成功！");
        } catch (IOException e) {
            System.out.println("InitFinish失败！");
            e.printStackTrace();
        }
    }

    public static CommandControl getInstance() {
        if (commandControl == null) {
            synchronized (CommandControl.class) {
                if (commandControl == null) {
                    commandControl = new CommandControl();
                }
            }
        }
        return commandControl;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                try {
                    byte[] commandBytes = new byte[10240];
                    getCommandStream.read(commandBytes);
                    if (commandBytes.length > 0) {
                        String command = (new String(commandBytes)).trim();
                        String commandHead = command.split("@")[0].trim();

                        if (commandHead.equals("STU"))//接收到的是设置测试台位命令SettingTestUnit
                        {
                            System.out.println(command);
                            if (command.split("@").length != 1) {
                                String commandBody = command.split("@")[1];
                                String testingUnits[] = commandBody.split(",");
                                GlobalVariable.testingUnitsID.clear();
                                for (int i = 0; i < testingUnits.length; i++) {
                                    //0位台位号，1位洗衣机类型
                                    String testUnitAndModelType[] = testingUnits[i].split("#");
                                    GlobalVariable.testingUnitsID.put(testUnitAndModelType[0], testUnitAndModelType[1]);
                                    GlobalVariable .ips .put(testUnitAndModelType[0],testUnitAndModelType[2]) ;
                                    MainControl.getMainControl().reInit(Integer.parseInt(testUnitAndModelType[0]));
                                }

                            }
                        }
                        if (commandHead.equals("START")) {
                            System.out.println(command);
                            String commandTaiweiNo = command.split("@")[1].trim();
                            String testUnitsAndModelType[] = commandTaiweiNo.split("#");
                            if (!GlobalVariable.testingUnitsID.containsKey(commandTaiweiNo)) {
                                GlobalVariable.testingUnitsID.put(testUnitsAndModelType[0], testUnitsAndModelType[1]);
                                GlobalVariable.ips.put(testUnitsAndModelType[0],testUnitsAndModelType[2]);
                                MainControl.getMainControl().reInit(Integer.parseInt(testUnitsAndModelType[0]));
                            }

                        }
                        if (commandHead.equals("STOP")) {
                            System.out.println(command);
                            String commandTaiweiNo = command.split("@")[1].trim();
                            String testUnitsAndModelType[] = commandTaiweiNo.split("#");
                            GlobalVariable.testingUnitsID.remove(testUnitsAndModelType[0]);
                            GlobalVariable.ips .remove(testUnitsAndModelType[0]);
                            MainControl.getMainControl().closeSocket(Integer.parseInt(testUnitsAndModelType[0]));
                        }
                        //设置结束后关闭采集
                        synchronized (this) {
                            if (commandHead.equals("END")) {
                                System.exit(0);
                            }
                        }

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
